package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.QueryPostGradesListReq;
import com.sg.dto.base.framework.req.QuerypostGradesDetailReq;
import com.sg.entity.StructureStationGrade;
import com.sg.service.base.framework.MStructureStationGradeService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.StructureStationGradeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MStructureStationGradeServiceImpl
        extends ServiceImpl<StructureStationGradeMapper, StructureStationGrade>
        implements MStructureStationGradeService {

    @Autowired
    StructureStationGradeMapper structureStationGradeMapper;

    /**
     * 1-3-02查询岗位等级列表[132]
     */
    @Trace(operationName = "1-3-02查询岗位等级列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<StructureStationGrade> queryPostGradesList(QueryPostGradesListReq reqDto) {

        try {
            QueryWrapper<StructureStationGrade> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(StructureStationGrade.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(StructureStationGrade.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(StructureStationGrade.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<StructureStationGrade>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }


    /**
     * 1-3-02删除岗位等级[130]
     */
    @Trace(operationName = "1-3-02删除岗位等级")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletepostGrades(String stationGradeId) {

        return super.removeById(stationGradeId);
    }

    /**
     * 1-3-02修改岗位等级[129]
     */
    @Trace(operationName = "1-3-02修改岗位等级")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatepostGrades(StructureStationGrade structureStationGrade) {

        return super.updateById(structureStationGrade);
    }

    /**
     * 1-3-02新增岗位等级[128]
     */
    @Trace(operationName = "1-3-02新增岗位等级")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addpostGrades(StructureStationGrade structureStationGrade) {

        if (super.save(structureStationGrade)) {
            return structureStationGrade.getStationGradeId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-3-02查询岗位等级[131]
     */
    @Trace(operationName = "1-3-02查询岗位等级")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StructureStationGrade querypostGradesDetail(QuerypostGradesDetailReq reqDto) {

        try {

            QueryWrapper<StructureStationGrade> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<StructureStationGrade> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                StructureStationGrade retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
